//    Dhammapada
//    Copyright (C) 2007 Sakya
//    sakya_tg@yahoo.it
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#include <stdio.h>
#include "opendir.h"
#include "dhammapada.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Private functions:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void clearIndex(){
	int i;
	for (i=0; i < categoriesIndex.count; i++){
		strcpy(categoriesIndex.cat[i].fileName, "");
		categoriesIndex.cat[i].index = 0;
		strcpy(categoriesIndex.cat[i].title, "");
	}
	categoriesIndex.count = 0;
	categoriesIndex.totalSections = 0;
}

void clearCategory(struct oneCategory *cat){
    int i, j;
    strcpy(cat->fileName, "");
    strcpy(cat->title, "");
    cat->index = 0;
    cat->sectionsNumber = 0;
    cat->minSection = 0;
    cat->maxSection = 0;
    for (i=0; i<MAX_SECTIONS; i++){
        cat->sections[i].index = 0;
        cat->sections[i].rowsNumber = 0;
        for (j=0; j<MAX_SECTION_ROWS; j++)
            strcpy(cat->sections[i].rows[j], "");
    }
}

int _loadCategory(char *fileName, struct oneCategory *targetCategory){
	FILE *file;
    char lineText[256];
    int i;
    int count = 0;
    int currentSection = -1;
    char number[5] = "";
    
    file = fopen(fileName, "r");
    if (file == NULL)
        return -1;

    strcpy(targetCategory->fileName, fileName);
	while(fgets(lineText, 256, file) != NULL){
        if (strstr(lineText, "#N=") != NULL){
            count = 0;
            strcpy(number, "");
            for (i=3; i < strlen(lineText); i++){
    			if ((int)lineText[i] == 10 || (int)lineText[i] == 13)
    				continue;
                number[count++] = lineText[i];
                number[count] = '\0';
            }
            currentSection = atoi(number);
            targetCategory->sectionsNumber++;
            targetCategory->sections[targetCategory->sectionsNumber - 1].rowsNumber = 0;
            targetCategory->sections[targetCategory->sectionsNumber - 1].index = currentSection;
            if (currentSection > targetCategory->maxSection)
                targetCategory->maxSection = currentSection;
            if (!targetCategory->minSection || currentSection < targetCategory->minSection)
                targetCategory->minSection = currentSection;
        }else if (lineText[0] != '#' && currentSection != -1){
            if ((int)lineText[strlen(lineText) - 1] == 10 || (int)lineText[strlen(lineText) - 1] == 13)
                lineText[strlen(lineText) - 1] = '\0';
            if ((int)lineText[strlen(lineText) - 1] == 10 || (int)lineText[strlen(lineText) - 1] == 13)
                lineText[strlen(lineText) - 1] = '\0';
            if (strlen(lineText))
                strcpy(targetCategory->sections[targetCategory->sectionsNumber - 1].rows[targetCategory->sections[targetCategory->sectionsNumber - 1].rowsNumber++], lineText);
        }
    }
    fclose(file);
    return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// loadCategoriesIndex:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int loadCategoriesIndex(char *lang){
	struct opendir_struct dhDir;
	char buffer[262];
	char ext[1][4] = {"TXT"};
	int i = 0;
    int categoriesRed = 0;
    char lineText[256];
    struct settings *userSettings;
	FILE *file;
	
	userSettings = SETTINGS_get();
	clearIndex();
	sprintf(buffer, "%s/data/%s", userSettings->ebootPath, lang);
	char *result = opendir_open(&dhDir, buffer, ext, 1, 0);
	if (result == 0){
    	sortDirectory(&dhDir);

        for (i = 0; i < dhDir.number_of_directory_entries; i++){
            sprintf(buffer, "%s/data/%s/%s", userSettings->ebootPath, lang, dhDir.directory_entry[i].d_name);
            file = fopen(buffer, "r");
            //Search for title:
        	while(fgets(lineText, 256, file) != NULL){
                if (strstr(lineText, "#TITLE=") != NULL){
                    int j;
                    int countTitle = 0;
                    struct category cat;
                    cat.index = categoriesRed++;
                    cat.sectionsNumber = 0;
                    strcpy(cat.fileName, buffer);
                    for (j=7; j < strlen(lineText); j++){
            			if ((int)lineText[j] == 10 || (int)lineText[j] == 13)
            				continue;
                        cat.title[countTitle++] = lineText[j];
                        cat.title[countTitle] = '\0';
                    }
                    categoriesIndex.cat[categoriesIndex.count++] = cat;
                }else if (strstr(lineText, "#N=") != NULL && categoriesIndex.count){
                    categoriesIndex.cat[categoriesIndex.count - 1].sectionsNumber++;
                    categoriesIndex.totalSections++;
                }
            }
            fclose(file);
        }
    }
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// loadCategoriesIndexOriginal:
// Loads the categorie's index of the original language
// WARNING: it clears also the "other language" categorie's index.
//          Call this BEFORE loadCategoriesIndex!
//          The categorie's number must be the same in all languages
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int loadCategoriesIndexOriginal(){
    clearIndex();
    loadCategoriesIndex(ORIGNAL_LANG);
    categoriesIndexOriginal = categoriesIndex;
    clearIndex();
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// loadCategory: loads a single category
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int loadCategory(int index){
    clearCategory(&currentCategory);
    clearCategory(&currentCategoryOriginal);

    _loadCategory(categoriesIndex.cat[index].fileName, &currentCategory);
    strcpy(currentCategory.title, categoriesIndex.cat[index].title);
    currentCategory.index = index;
    _loadCategory(categoriesIndexOriginal.cat[index].fileName, &currentCategoryOriginal);
    strcpy(currentCategoryOriginal.title, categoriesIndexOriginal.cat[index].title);
    currentCategoryOriginal.index = index;
    return 0;
}
